package com.starsea.book.ui.newpage.adapter

import android.content.Context
import android.os.Bundle
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.starsea.book.base.adapter.ItemViewHolder
import com.starsea.book.base.adapter.RecyclerAdapter
import com.starsea.book.databinding.ItemHomeCategoryParentBinding
import com.starsea.book.ui.newpage.entity.CategoryEntity
import com.starsea.book.ui.widget.recycler.ItemTouchCallback

class CategoryParentAdapter(
    context: Context,
    var onChanged: ((value: CategoryEntity.CategoryChildEntity) -> Unit)? = null
) :
    RecyclerAdapter<CategoryEntity, ItemHomeCategoryParentBinding>(context),
    ItemTouchCallback.Callback {


    override fun getViewBinding(parent: ViewGroup): ItemHomeCategoryParentBinding {
        return ItemHomeCategoryParentBinding.inflate(inflater, parent, false)
    }

    override fun registerListener(holder: ItemViewHolder, binding: ItemHomeCategoryParentBinding) {
    }


    override fun convert(
        holder: ItemViewHolder,
        binding: ItemHomeCategoryParentBinding,
        item: CategoryEntity,
        payloads: MutableList<Any>
    ) {
        binding.run {
            val bundle = payloads.getOrNull(0) as? Bundle
            if (bundle == null) {
                tvCategoryName.text = item.bigTypeTitle
                val adapterChild = CategoryChildAdapter(context, onChanged)
                rvContent.layoutManager = GridLayoutManager(context, 2)
                rvContent.adapter = adapterChild
                adapterChild.setItems(items = item.secTypeDtoOutList)
            } else {
                bundle.keySet().map {
                    when (it) {
                        "bigTypeTitle" -> tvCategoryName.text = item.bigTypeTitle
                    }
                }
            }
        }
    }


}